package com.fcscs.ecms.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity.DBLoc;
import com.fcscs.cservice.domain.Advertisement;
import com.fcscs.cservice.ws.WebServiceUtils;
import com.fcscs.ecms.dao.ECMSAdvertiseDao;

@SuppressWarnings("unchecked")
@Repository("ecmsAdvertiseDao")
public class ECMSAdvertiseDaoHibernate implements ECMSAdvertiseDao {

	
	@Autowired
	private ConnectionManager connMgr;
	private static DBLoc ADVDBLoc = new Advertisement().getDBLoc();
	
	private static final String FIND_ADVERTISES = "FROM Advertisement ad Where ad.orgId=:orgId and ad.propertyId=:propId ";
	private static final String UP_DELETE_ACTIVE = "Update Advertisement ad set ad.active=false where ad.id=:id";
	
	@Override
	public Advertisement getAdvertisement(long id) {
		return (Advertisement) connMgr.getCurrentSession(ADVDBLoc).get(Advertisement.class, id);
	}
	@Override
	public List<Advertisement> getAdvertisementList(long orgId, long propId,boolean isCurrent) {
		
		Query query= connMgr.getCurrentSession(ADVDBLoc).createQuery(FIND_ADVERTISES);
		query.setLong("orgId", orgId);
		query.setLong("propId", propId);
		
		return query.list();
	}

	@Override
	public List<Advertisement> getAdvertisementList(long orgId, long propId,long versionId) {
		
		Query query= connMgr.getCurrentSession(ADVDBLoc).createQuery(FIND_ADVERTISES);
		query.setLong("orgId", orgId);
		query.setLong("propId", propId);
		
		return query.list();
	}

	@Override
	public List<Advertisement> saveAdvertisementList(List<Advertisement> adList) {

		Session session = connMgr.getCurrentSession(ADVDBLoc);
		for(Advertisement at : adList){
			session.saveOrUpdate(at);
		}
		
		return adList;
	}

	@Override
	public Advertisement createAdvertisement(Advertisement ad) {
			connMgr.getCurrentSession(ADVDBLoc).save(ad);
		return ad;
	}

	@Override
	public Advertisement saveAdvertisement(Advertisement ad) {
		 connMgr.getCurrentSession(ADVDBLoc).saveOrUpdate(ad);
		return ad;
	}

	@Override
	public boolean deleteAdvertisement(Advertisement ad) {
		Query q = connMgr.getCurrentSession(ADVDBLoc).createQuery(UP_DELETE_ACTIVE);
		q.setLong("id", ad.getId());
		
		return (q.executeUpdate()== 1);
	}

	@Override
	public boolean permDeleteAdvertisement(Advertisement ad) {
		try {
			connMgr.getCurrentSession(ADVDBLoc).delete(ad);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<com.fcscs.cservice.domain.model.Advertisement> getAdvertisementViewList(long orgId, long propId) {
		
		//TODO unit test
		String sql = "select a.id, a.url, i.image from Advertisement a ,ImageResource i " +
					"where a.bannerImageId=i.id and a.orgId=:orgId and a.propertyId=:propId";
		
		Query sqlQu = connMgr.getCurrentSession(ADVDBLoc).createQuery(sql);
		sqlQu.setParameter("orgId", orgId);
		sqlQu.setParameter("propId", orgId);
		
		List<com.fcscs.cservice.domain.model.Advertisement> list = new ArrayList<com.fcscs.cservice.domain.model.Advertisement>();
		List<Object[]> categories = (List<Object[]>)sqlQu.list();
		for(Object[] o : categories) {
			com.fcscs.cservice.domain.model.Advertisement adver = new com.fcscs.cservice.domain.model.Advertisement();
			 adver.setAdvertismentId((Long) o[0]);
			 adver.setUrl((String) o[1]);
			 adver.setBannerFile(WebServiceUtils.convertToString((byte[])o[2]));
			 list.add(adver);
		 }
		return list;
	}

}
